/**
 * Mar 28, 2010
 */
package org.tomale.id.documents.management.ui;

import java.util.List;

import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.tomale.id.ProviderException;
import org.tomale.id.documents.Document;
import org.tomale.id.documents.DocumentType;
import org.tomale.id.documents.management.Activator;
import org.tomale.id.documents.management.DocumentManager;
import org.tomale.id.documents.management.IDocumentStore;

/**
 * @author ferd
 *
 */
public class DocumentBrowser extends ViewPart {

	public final static String VIEW_ID = "org.tomale.id.documents.management.documentbrowser";
	
	Combo _types;
	Text _sz;
	Button _find;
	TableViewer _vwr;

	IDocumentStore _store;
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite comp = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		comp.setLayout(layout);
		
		_types = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
		_types.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				false,false));
		initTypes();
		
		_sz = new Text(comp, SWT.BORDER | SWT.SEARCH);
		_sz.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				true,false));
		
		_find = new Button(comp, SWT.PUSH);
		_find.setImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "img/search.gif").createImage());
		_find.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				false,false));
		_find.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				doFind();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				
			}
		});
		
		_vwr = new TableViewer(comp);
		_vwr.setContentProvider(new ContentProvider());
		_vwr.setLabelProvider(new LabelProvider());
		initTable();
		
		getSite().setSelectionProvider(_vwr);
		
		if(_types.getItemCount() == 0){
			_types.setEnabled(false);
			_sz.setEnabled(false);
			_find.setEnabled(false);
			_vwr.getTable().setEnabled(false);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		_sz.setFocus();
	}
	
	private void setErrorMessage(String message){
		getViewSite().getActionBars().getStatusLineManager().setErrorMessage(message);
	}
	
	private void initTypes(){
		IDocumentStore store = getStore();
		if(store != null){
			List<DocumentType> types = store.getDocumentTypes();
			if(_types == null){
				for(DocumentType t : types){
					_types.add(t.getValue());
				}
			} else {
				Activator.log(Status.ERROR, "Unable to retrieve document types. " +
						"Document store returned null.");
				setErrorMessage("Unable to retrieve document types.");
			}
		}
	}
	
	private IDocumentStore getStore(){
		if(_store == null){
			try {
				_store = DocumentManager.getStore();
				if(_store == null){
					String message = "Document store is not properly configured.";
					Activator.log(Status.ERROR, message);
				}
			}catch(ProviderException e){
				Activator.log(Status.ERROR, e.getMessage());
				setErrorMessage(e.getMessage());
			}
		}
		return _store;
	}
	
	/**
	 * create table columns
	 */
	private void initTable(){
		Table tbl = _vwr.getTable();
		tbl.setHeaderVisible(true);
		tbl.setLinesVisible(true);
		tbl.setLayoutData(new GridData(SWT.FILL,SWT.FILL,
				true,true,3,1));
		
		TableColumn col;
		
		col = new TableColumn(tbl, SWT.NONE);
		col.setText("Id");
		col.setToolTipText("Document Id");
		col.setWidth(20);
		col.setResizable(true);
		col.setMoveable(true);
		
		col = new TableColumn(tbl, SWT.NONE);
		col.setText("Keywords");
		col.setToolTipText("Document keywords");
		col.setWidth(100);
		col.setResizable(true);
		col.setMoveable(true);
	}
	
	private boolean valid(){
		return true;
	}
	
	private void doFind(){
		if(valid()){
			// TODO lock down ui, spawn thread, do search
		} else {
			// TODO prompt if not valid
		}
	}
	
	private class ContentProvider implements IStructuredContentProvider {

		@Override
		public Object[] getElements(Object inputElement) {
			return ((List<Document>) inputElement).toArray();
		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private class LabelProvider extends StyledCellLabelProvider 
		implements ITableLabelProvider {

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			// TODO Auto-generated method stub
			return null;
		}
		
	}
}
